/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package topcoder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

/**
 *
 * @author Mohamed.Azouz
 */
public class MedalTable {

    public String[] generate(String[] results) {
        HashMap<String, Country> hm = new HashMap<String, Country>();

        for (int i = 0; i < results.length; i++) {
            String[] rows = results[i].split(" ");
            int j = 0;
            for (String row : rows) {
                Country c;
                if (hm.containsKey(row)) {
                    c = hm.get(row);

                } else {
                    c = new Country();
                }
                if (j == 0) {
                    c.g++;
                }
                if (j == 1) {
                    c.s++;
                }
                if (j == 2) {
                    c.b++;
                }
                c.name = row;
                hm.put(row, c);
                j++;
            }
        }
        ArrayList<Country> countrys = new ArrayList<Country>(hm.values());
        Collections.sort(countrys);
        System.out.println(countrys);
        String[] finalS = new String[countrys.size()];
        Country tmp = countrys.get(0);
        countrys.set(0, countrys.get(countrys.size() - 1));
        countrys.set(countrys.size() - 1, tmp);
        for (int i = 0; i < countrys.size(); i++) {
            finalS[i] = countrys.get(i).toString();
        }
        return finalS;
    }

    public static void main(String[] args) {
        String results[] = {"USA AUT ROM"};
        MedalTable m = new MedalTable();
        System.out.println(m.generate(results).toString());;


    }
}

class Country implements Comparable<Country> {

    String name;
    int g = 0;
    int s = 0;
    int b = 0;

    @Override
    public int compareTo(Country o) {
        if (g > o.g) {
            return 0;
        }
        if (o.g > g) {
            return 1;
        }
        if (o.g == g) {
            if (s > o.s) {
                return 0;
            }
            if (s < o.s) {
                return 1;
            }
            if (s == o.s) {
                if (b > o.b) {
                    return 0;
                }
                if (b < o.b) {
                    return 1;
                }
                if (b == o.b) {
                    return name.compareTo(o.name);
                }
            }
        }
        return 0;
    }

    @Override
    public String toString() {
        return name + " " + g + " " + s + " " + b;
    }
}
